//
//  DCDispatchTableViewCell.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/28.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCDispatchTableViewCell.h"
@interface DCDispatchTableViewCell ()
@property (nonatomic,strong) UIView *firstView;
@property (nonatomic,strong) UIImageView *carrierImage;
@property (nonatomic,strong) UILabel *carrierLabel;//承运商
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *stateLabel;

@property (nonatomic,strong) UIView *secondView;
@property (nonatomic,strong) DCImageAndTextLabel *startLabel;
@property (nonatomic,strong) DCImageAndTextLabel *extractLabel;
@property (nonatomic,strong) DCImageAndTextLabel *endLabel;
@property (nonatomic,strong) DCImageAndTextLabel *sendLabel;

@property (nonatomic,strong) UIImageView *pointImage;

@property (nonatomic,strong) DCImageAndTextLabel *carLabel;
@property (nonatomic,strong) UILabel *totalNumLabel;
@property (nonatomic,strong) UILabel *numLabel;

@property (nonatomic,strong) UIButton *coverBtn;
@end
@implementation DCDispatchTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpBaseUI];
    }
    return self;
}
-(void)setUpBaseUI
{
    UIView *firstView = [[UIView alloc]init];
    firstView.backgroundColor = [UIColor whiteColor];
    self.firstView = firstView;
    [self addSubview:firstView];
    
    UIImageView *carrierImage = [[UIImageView alloc]initWithImage:IMAGENAME(@"用户头像")];
    //    carrierImage.backgroundColor = [UIColor redColor];
    self.carrierImage = carrierImage;
    [self.firstView addSubview:carrierImage];
    
    self.carrierLabel = [self getLabelWithFont:17 textAlignment:NSTextAlignmentLeft];
    [self.firstView addSubview:self.carrierLabel];
    
    self.timeLabel = [self getLabelWithFont:14 textAlignment:NSTextAlignmentCenter];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    [self.firstView addSubview:self.timeLabel];
    
    self.stateLabel = [self getLabelWithFont:17 textAlignment:NSTextAlignmentRight];
    [self.firstView addSubview:self.stateLabel];
    
    UIView *secondView = [[UIView alloc]init];
    secondView.backgroundColor = [UIColor whiteColor];
    self.secondView = secondView;
    [self addSubview:secondView];
    
    DCImageAndTextLabel *startLabel = [[DCImageAndTextLabel alloc]init];
    startLabel.imageView.image = IMAGENAME(@"起点");
    self.startLabel = startLabel;
    [self.secondView addSubview:startLabel];
    
    DCImageAndTextLabel *extractLabel = [[DCImageAndTextLabel alloc]init];
    extractLabel.imageView.image = IMAGENAME(@"提现");
    self.extractLabel = extractLabel;
    [self.secondView addSubview:extractLabel];
    
    DCImageAndTextLabel *endLabel = [[DCImageAndTextLabel alloc]init];
    endLabel.imageView.image = IMAGENAME(@"终点");
    self.endLabel = endLabel;
    [self.secondView addSubview:endLabel];
    
    DCImageAndTextLabel *sendLabel = [[DCImageAndTextLabel alloc]init];
    sendLabel.imageView.image = IMAGENAME(@"送");
    self.sendLabel = sendLabel;
    [self.secondView addSubview:sendLabel];
    
    self.carLabel = [[DCImageAndTextLabel alloc]init];
    self.carLabel.imageView.image = IMAGENAME(@"立方体");
//    self.carLabel.textLabel.font = FONT(17);
    [self.secondView addSubview:self.carLabel];
    
    self.totalNumLabel = [self getLabelWithFont:15 textAlignment:NSTextAlignmentRight];
    [self.secondView addSubview:self.totalNumLabel];
    
    self.numLabel = [self getLabelWithFont:14 textAlignment:NSTextAlignmentLeft];
    self.numLabel.textColor = [UIColor lightGrayColor];
    [self.secondView addSubview:self.numLabel];
    
    self.pointImage = [[UIImageView alloc]initWithImage:IMAGENAME(@"箭头")];
    //    self.pointImage.backgroundColor = [UIColor redColor];
    [self addSubview:self.pointImage];
    
    UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectedBtn setImage:IMAGENAME(@"choose") forState:UIControlStateNormal];
    [selectedBtn setImage:IMAGENAME(@"choose-selected") forState:UIControlStateSelected];
    [selectedBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.selectedBtn = selectedBtn;
    [self.secondView addSubview:selectedBtn];
    
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        coverBtn.backgroundColor = [UIColor yellowColor];
    [coverBtn addTarget:self action:@selector(coverBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.coverBtn = coverBtn;
    [self.secondView addSubview:coverBtn];
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat kMargin = 10;
    //    CGFloat kHeight = 40;
    self.firstView.frame = CGRectMake(0, kMargin, self.width, kBaseHeight);
    
    self.carrierImage.frame = CGRectMake(10, 3, self.firstView.height - 6, self.firstView.height - 6);
    self.carrierImage.layer.cornerRadius = self.carrierImage.width/2;
    
    self.carrierLabel.frame = CGRectMake(self.carrierImage.right+3, 0, 70*WIDTHSCALE6, self.firstView.height);
    
    self.timeLabel.width = 150*WIDTHSCALE6;
    self.timeLabel.height = self.firstView.height;
    self.timeLabel.y = 0;
    self.timeLabel.centerX = self.firstView.centerX;
    
    self.stateLabel.frame = CGRectMake(self.firstView.width - 70*WIDTHSCALE6, 0, 70*WIDTHSCALE6-3, self.firstView.height);
    
    self.secondView.frame = CGRectMake(0, self.firstView.bottom+1, self.firstView.width, (kBaseHeight-10)*3);
    
    self.startLabel.frame = CGRectMake(30, 0, (self.secondView.width - 30)/3, kBaseHeight-10);
    self.extractLabel.frame = CGRectMake(self.startLabel.right, 0, (self.secondView.width - 30)*2/3, self.startLabel.height);
    self.endLabel.frame = CGRectMake(self.startLabel.x, self.startLabel.bottom, self.startLabel.width, self.startLabel.height);
    self.sendLabel.frame = CGRectMake(self.extractLabel.x, self.endLabel.y, self.extractLabel.width, self.startLabel.height);
    
    self.carLabel.frame = CGRectMake(20, self.sendLabel.bottom, 100*WIDTHSCALE6, self.secondView.height/3);
    self.totalNumLabel.frame = CGRectMake(self.carLabel.right, self.carLabel.y, 80*WIDTHSCALE6, self.carLabel.height);
    self.numLabel.frame = CGRectMake(self.totalNumLabel.right, self.carLabel.y, self.secondView.width - self.carLabel.width - self.totalNumLabel.width, self.carLabel.height);
    
    self.pointImage.size = CGSizeMake(20, 20);
    self.pointImage.x = self.width - self.pointImage.width;
    self.pointImage.centerY = self.secondView.centerY;
    
    CGFloat WIDTH = 15;
    self.selectedBtn.frame = CGRectMake(10, (self.secondView.height*2/3 - WIDTH)/2, WIDTH, WIDTH);
    self.coverBtn.frame = CGRectMake(0, 0, 50, self.secondView.height*2/3);
}

-(void)setModel:(DCDispatchModel *)model
{
    _model = model;
    self.carrierLabel.text = model.carrName;
    self.timeLabel.text = model.endDate;
//    if ([model.orderStatus isEqualToString:@""]) {
//
//    }
    self.stateLabel.text = @"待调度";
    self.stateLabel.textColor = [UIColor redColor];
    
    self.startLabel.textLabel.text = model.startCity;
    self.extractLabel.textLabel.text = [model.startAddress cutStrWithLength:10];
    self.endLabel.textLabel.text = model.endCity;
    self.sendLabel.textLabel.text = [model.endAddress cutStrWithLength:10];
    
    self.carLabel.textLabel.text = @"新车";
    self.totalNumLabel.text = [NSString stringWithFormat:@"%@辆",model.totalCar];
    self.numLabel.attributedText = [YLAttributedStr changNumberColorWithTotalString:[NSString stringWithFormat:@"(SUV%@CAR%@)",model.suvNumber,model.carNumber] andColor:BlueColor];
}
//复选框点击事件
- (void)agreeBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if ([_delegate respondsToSelector:@selector(selectedTableViewCell:selectedAgreeRow:)]) {
        [_delegate selectedTableViewCell:self selectedAgreeRow:sender.tag];
    }
}
-(void)coverBtnClick:(UIButton *)button
{
    [self agreeBtnClick:_selectedBtn];
}


-(UILabel *)getLabelWithFont:(NSInteger)font textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc]init];
    label.font = FONT(font);
    label.textAlignment = textAlignment;
    return label;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
